package cn.rengy.web.generate;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.rengy.web.framework.generate.service.GenerateService;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateNotFoundException;

@Service
public class CodeGenerator {

	@Autowired
	private Configuration freeMarkerConfiguration;
	
	@Autowired
	private GenerateService generateService;
	
	
	public void  generate(Map<String,Object> param) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException{
		//List<Map<String,Object>> tables=generateService.getAllTables();
		List<Map<String,Object>> tables=generateService.getTable("dcs_document");
		for(Map<String,Object> table :tables) {
			Map<String,Object> data=new HashMap<String,Object>();
			String table_name=(String)table.get("TABLE_NAME");
			String table_comment=(String)table.get("TABLE_COMMENT");
			
			String bean_name=_Utils.getBeanName(table_name);
			data.put("bean_name", bean_name);
			String bean_type_name=_Utils.getBeanTypeName(table_name);
			data.put("bean_type_name", bean_type_name);
			
			String model_code=_Utils.getCode(table_name);
			data.put("model_code", model_code);
			List<Map<String, Object>> columnList=generateService.getColumnList(table_name);
			for(Map<String, Object> column:columnList){
				String column_name=(String)column.get("COLUMN_NAME");
				
				String column_bean_name=_Utils.getBeanName(column_name);
				column.put("bean_name", column_bean_name);
				String column_bean_type_name=_Utils.getBeanTypeName(column_name);
				column.put("bean_type_name", column_bean_type_name);
			}
			
			data.put("table", table);
			data.put("columnList", columnList);
			String servicePath=System.getProperty("user.dir")+"\\src\\main\\java\\com\\suchuang\\dcs\\service";
			File a=new File(servicePath);
			if(!a.exists()) {
				a.mkdirs();
			}
			File serviceTargetFile=new File(servicePath,"Front"+bean_type_name+"Service.java");
			generateService.write(serviceTargetFile, data, generateService.getTemplate("/Service.ftl"));
			
			String paramBasePath=System.getProperty("user.dir")+"\\src\\main\\java\\com\\suchuang\\dcs\\po";
			File b=new File(paramBasePath);
			if(!b.exists()) {
				b.mkdirs();
			}
			File paramBaseTargetFile=new File(paramBasePath,bean_type_name+"ParamBase.java");
			generateService.write(paramBaseTargetFile, data, generateService.getTemplate("/ParamBase.ftl"));
			
			
			String paramPath=System.getProperty("user.dir")+"\\src\\main\\java\\com\\suchuang\\dcs\\po";
			File c=new File(paramPath);
			if(!c.exists()) {
				c.mkdirs();
			}
			File targetFile=new File(paramPath,bean_type_name+"Param.java");
			generateService.write(targetFile, data, generateService.getTemplate("/Param.ftl"));
		
			//生成Controller
			String controllerPath=System.getProperty("user.dir")+"\\src\\main\\java\\com\\suchuang\\dcs\\controller";
			File d=new File(controllerPath);
			if(!d.exists()) {
				d.mkdirs();
			}
			File controller=new File(controllerPath,"Front"+bean_type_name+"Controller.java");
			generateService.write(controller, data, generateService.getTemplate("/Controller.ftl"));
			
			
			String voPath=System.getProperty("user.dir")+"\\src\\main\\java\\com\\suchuang\\dcs\\po";
			File e=new File(voPath);
			if(!e.exists()) {
				e.mkdirs();
			}
			File voFile=new File(voPath,bean_type_name+"VO.java");
			generateService.write(voFile, data, generateService.getTemplate("/VO.ftl"));
			
		}
		
	}
	public static void main(String args[]){
		Map<String,Object> data=new HashMap<String,Object>();
		data.put("model_name", "文档");
		data.put("model_code", "document");
		data.put("model_code_lowercase", "dcsDocumentReceiver");
		data.put("model_code_uppercase", "DcsDocumentReceiver");
//		data.put("", );
//		data.put("", );
//		data.put("", );
//		data.put("", );
//		data.put("", );
//		data.put("", );
		
		String modelName="DcsDocumentReceiver";
		
		//System.out.println(bathpath);
		
		//create(bathpath+modelName+"Controller.java","controller.ftl",data);
		//create(bathpath+modelName+"Service.java","Service.ftl",data);
		//create(bathpath+modelName+"ServiceImpl.java","ServiceImpl.ftl",data);
	}
}
